<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>PHP Basics</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="PHP, learn PHP, basics, tutorial, programming language">
<meta name="description" content="In this part of the PHP tutorial, we cover PHP basics.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">


<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div class="content">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>PHP Basics</h1>

<p>
In this part of the PHP tutorial, we will talk about basic programming in PHP.
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* top_horizontal */
google_ad_slot = "3327173442";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
All the PHP code is surrounded by two delimiters, <code>&lt;?php</code> and 
<code>?&gt;</code>. The 'php' string
is optional but recommended. 
</p>

<pre class="code">
&lt;?php 

# PHP code

?&gt;
</pre>

<p>
PHP code is put between two delimiters. 
</p>

<pre class="code">
&lt;?php 

$a = 23;
print $a;

?&gt;
</pre>

<p>
This PHP script assigns a value to a variable. It prints it to the
console. Note that we say console, because here we use
the PHP_CLI command line interpreter. If you test these examples on the web, the output will
be sent to the browser. 
</p>

<pre class="explanation">
$a = 23;
</pre>

<p>
We assign a value 23 to the $a variable. Each variable starts with a dollar character.
This PHP code line is a statement. Each statement
ends with a semicolon. In PHP, semicolons are not mandatory, like in Javascript or Ruby. 
They are obligatory. 
</p>

<pre class="explanation">
print $a;
</pre>

<p>
We print the $a variable to the console. The <code>print</code> keyword
does not add a new line to the output. If we want a new line, we must put it manually.
<code>print</code> keyword takes only one argument. 
</p>

<pre class="code">
&lt;?php 

$a = 23;
$b = 24;

echo $a, "\n", $b, "\n";

?&gt;
</pre>

<p>
In this script, we use the <code>echo</code> keyword. It is similar to the
<code>print</code> keyword. Unlike the <code>print</code> keyword, 
it can take multiple arguments.
</p>

<pre class="explanation">
$a = 23;
$b = 24;
</pre>

<p>
We define two variables.
</p>

<pre class="explanation">
echo $a, "\n", $b, "\n";
</pre>

<p>
We print the variables to the console. We also include the new line characters.
Arguments can be separated by commas. 
</p>

<pre>
$ php echo.php 
23
24
</pre>

<p>
This is the output of the script. 
</p>

<h2>Types</h2>

<p>
PHP is a weakly typed language. It works with types, but the
programmer does not specify them when declaring variables.
A <b>data type</b> is a one of various types of data, as double, integer, or boolean.
Values of a certain data type are from a specific range of values stating the possible values for that type, 
the operations that can be done on that type, and the way the values of that type are stored.
PHP works implicitly with data types. Programmers do not specify explicitly the data types. 
</p>

<pre class="code">
&lt;?php 

$a = "Jane";
echo "$a \n";

$a = 12;
echo "$a \n";

$a = 56.4;
echo "$a \n";

$a = true;
echo "$a \n";

?&gt;
</pre>

<p>
In this PHP script, we have an $a variable. First, we assign it a
string, then an integer, a double and finally a boolean value. 
If we assign a string to a variable the PHP automatically creates
a string variable. 
</p>

<pre>
$ php dynamic.php 
Jane 
12 
56.4 
1 
</pre>

<p>
Running the script. 
</p>


<pre class="code">
&lt;?php 

$temperature = 12.4;
$name = "Jane";
$age = 17;
$values = array(1, 2, 3, 4, 5, 6); 

class Being {};

$somebody = new Being();

echo gettype($temperature), "\n";
echo gettype($name), "\n";
echo gettype($age), "\n";
echo gettype($values), "\n";
echo gettype($somebody), "\n";

?&gt;
</pre>

<p>
In the above PHP script, we dynamically create five types. 
</p>

<pre class="explanation">
$values = array(1, 2, 3, 4, 5, 6); 

class Being {};
</pre>

<p>
This is an array and a class. Both types will be covered later in 
more detail. 
</p>

<pre class="explanation">
echo gettype($temperature), "\n";
</pre>

<p>
The <code>gettype()</code> function returns the type
of the variable in question. 
</p>

<pre>
$ php gettype.php 
double
string
integer
array
object
</pre>

<p>
This script lists the basic types of the PHP language. 
</p>


<h2>Constants</h2>

<p>
In PHP, we can create constants. A <b>constant</b> is a name for a value that, unlike a variable, 
cannot be reassociated with a different value. We use the <code>define()</code> function to 
create constants in PHP.
</p>

<pre class="code">
&lt;?php 

define("BLUE", "0000FF");

echo BLUE, "\n";

echo defined("BLUE");
echo "\n";

?&gt;
</pre>

<p>
In this PHP script, we define a BLUE constant. 
</p>

<pre class="explanation">
define("BLUE", "0000FF");
</pre>

<p>
Here we define the BLUE constant. It is a convention to 
write constants in uppercase letters. 
</p>

<pre class="explanation">
echo BLUE, "\n";
</pre>

<p>
Here we use it. Note that constants are not preceded by
($) dollar character. 
</p>

<pre class="explanation">
echo defined("BLUE");
</pre>

<p>
We have used another function, the <code>defined()</code> function.
It checks, if a particular constant exists. Returns true, if it does. 
</p>

<pre>
$ php constant.php 
0000FF
1
</pre>

<p>
Running the example gives the above output.
</p>

<p>
PHP also has some predefined constants. 
</p>

<pre class="code">
&lt;?php 

echo TRUE;
echo "\n";
echo PHP_VERSION;
echo "\n";
echo PHP_OS;
echo "\n";
echo __LINE__;
echo "\n";
echo __FILE__;
echo "\n";
echo DIRECTORY_SEPARATOR;
echo "\n";
echo PHP_DATADIR;
echo "\n";

?&gt;
</pre>

<p>
Here we print some built-in PHP constants. For example, the <code>PHP_OS</code>
constant prints the OS version on which the PHP was built.
</p>

<pre>
$ php constants.php 
1
5.2.6-2ubuntu4.6
Linux
9
/home/vronskij/programming/php/basics/constants.php
/
${prefix}/share
</pre>

<p>
On my system, I get this output. 
</p>


<h2>Variable interpolation</h2>

<p>
Next, we will define interpolation. <b>Variable interpolation</b> is replacing
variables with their values inside string literals.
Another names for variable interpolation are: variable substitution and variable expansion.
</p>

<pre class="code">
&lt;?php 

$age = 17;

echo "Jane is $age years old\n";

?&gt;
</pre>

<p>
The $age variable is replaced with the value 17 in the string. 
</p>

<pre>
$ php interpolation.php 
Jane is 17 years old
</pre>

<pre class="code">
&lt;?php 

$age = 17;

echo 'Jane is $age years old\n';

?&gt;
</pre>

<p>
However, this does not work, if we use the single quotes. In this
case, no interpolation happens and no special characters are
working. 
</p>

<pre>
$ php interpolation2.php 
Jane is $age years old\n$ 
</pre>

<p>
We see a verbatim output of the string. 
</p>

<h2>Including files</h2>

<p>
PHP code is split in multiple files for bigger programs. We use
the <code>include</code> statement to join various PHP files.
</p>

<pre class="code">
&lt;?php 

define("VERSION", 1.12);

function get_max($x, $y) {
    if ($x > $y) {
        return $x;
    } else {
        return $y;
    }
}

?&gt;
</pre>

<p>
Let's say, we have a common.php file, in which we define some constants and functions. 
</p>

<pre class="code">
&lt;?php 

include "common.php";

echo "The version is " . VERSION . "\n";

$a = 5;
$b = 3;

echo get_max($a, $b), "\n";

?&gt;
</pre>

<p>
And we have another file, which wants to use the aforementioned definitions. 
</p>

<pre class="explanation">
include "common.php";
</pre>

<p>
We simply include the definitions to our file with the 
<code>include</code> keyword. We must specify the exact path
to the common.php file. In our simple case, both files are in the same
directory. 
</p>


<p>
This chapter covered some basics of the PHP language.
</p>


<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br> 


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified February 20, 2010  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

